package com.frxuexi.mine.view

import android.util.Log
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.AccountCircle
import androidx.compose.material.icons.filled.Lock
import androidx.compose.material3.Button
import androidx.compose.material3.Icon
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import com.blankj.utilcode.util.RegexUtils
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.lib_base.BaseTitle
import com.frxuexi.lib_base.UiState
import com.frxuexi.mine.intent.UserIntent
import com.frxuexi.mine.viewmodel.UserViewModel
import com.tencent.mmkv.MMKV
import kotlinx.coroutines.launch

@Composable
fun LoginPage(navHostController: NavHostController,vm:UserViewModel= hiltViewModel()) {
    var uname by remember {
        mutableStateOf("0613")
    }
    var upass by remember {
        mutableStateOf("123456")
    }
    val kv = MMKV.defaultMMKV()
    LaunchedEffect(key1 = "", block = {
        launch {
            vm.state.collect{
                when(it){
                    is UiState.onSuccess<*> ->{
                        Log.i("===",it.data.toString())
                        kv.encode("token",it.data.toString())
                        kv.encode("uname",uname)
                        navHostController.popBackStack()
                    }

                    else -> {}
                }
            }
        }
    })
    Column(
        Modifier
            .fillMaxSize()
            .padding(20.dp)
    ) {
        val modifier = Modifier.fillMaxWidth()
        BaseTitle(navHostController = navHostController, title = "登录")
        Spacer(modifier = Modifier.height(160.dp))
        OutlinedTextField(
            modifier = modifier,
            value = uname,
            onValueChange = { uname = it },
            label = {
                Text(text = "用户名")
            },
            leadingIcon = {
                Icon(imageVector = Icons.Default.AccountCircle, contentDescription = "")
            })
        Spacer(modifier = Modifier.height(20.dp))
        OutlinedTextField(
            modifier = modifier,
            value = upass,
            onValueChange = { upass = it },
            label = {
                Text(text = "密码")
            },
            leadingIcon = {
                Icon(imageVector = Icons.Default.Lock, contentDescription = "")
            },
            visualTransformation = PasswordVisualTransformation()
        )
        Spacer(modifier = Modifier.height(30.dp))
        Button(modifier = modifier, onClick = {
            if(uname.isNullOrEmpty() || upass.isNullOrEmpty()){
                ToastUtils.showLong("输入内容不能为空")
                return@Button
            }
            /*if(!RegexUtils.isMobileSimple(uname)){
                ToastUtils.showLong("不是合法手机号")
                 return@Button
            }*/
            if(!uname.matches(Regex("[A-z0-9_]{4,9}"))){
                ToastUtils.showLong("不是合法用户名")
                return@Button
            }
            vm.sendIntent(UserIntent.login(mapOf("uname" to uname,"upass" to upass)))
        }) {
            Text(text = "登录")
        }
        Spacer(modifier = Modifier.height(20.dp))
        Text(
            text = "没有账号？去注册",
            color = Color.Blue,
            modifier = Modifier.align(alignment = Alignment.End)
        )
    }
}